﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    /// <summary>
    /// 会员查询委托函数
    /// </summary>
    /// <param name="customerID">返回用户ID</param>
    public delegate void EventSerchCustom(DataRow drCustomInfo);

    public partial class CustomSearch : BaseUserControl
    {
        private DataSet dsSerchCustomInfo = new DataSet();

        public EventSerchCustom onSerchCustom;
        /// <summary>
        /// 会员信息
        /// </summary>
        private DataSet dsCustomInfo = new DataSet();

        public CustomSearch()
        {
            InitializeComponent();
            string error = string.Empty;
            if (BussinessProxyManager.Instance != null)
            {
                BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustomInfo, out error);
            }
            if (dsCustomInfo != null)
            {
                dsSerchCustomInfo = dsCustomInfo.Copy();
            }
        }
        /// <summary>
        /// 重新刷新数据集
        /// </summary>
        public bool refershCustomInfo(out string error)
        {
            error = string.Empty;

            if (!BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustomInfo, out error))
            {
                return false;
            }

            if (dsCustomInfo != null)
            {
                dsSerchCustomInfo = dsCustomInfo.Copy();
            }
            return true;
        }

        private void textBoxDropDown1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals((char)Keys.Enter))
            {
                btnSearch_Click(sender, e);
            }
        }
        /// <summary>
        /// 搜索用户信息
        /// </summary>
        /// <param name="searchStr"></param>
        public void SearchCustomInfo(string searchStr)
        {
            DataTable dsSerchCustomInfo = searchCustomInfo(dsCustomInfo, searchStr);
            frmCustomQueryList frmCustomQuery = new frmCustomQueryList(dsSerchCustomInfo);
            if (frmCustomQuery.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            if (onSerchCustom != null)
            {
                onSerchCustom(frmCustomQuery.getCustomInfoRow);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.textEdit.Text == string.Empty)
            {
                MsgBox.MsgInfo("请输入正确的搜索信息。");
                return;
            }
            DataTable dsSerchCustomInfo = searchCustomInfo(dsCustomInfo, this.textEdit.Text);
            frmCustomQueryList frmCustomQuery = new frmCustomQueryList(dsSerchCustomInfo);
            if (frmCustomQuery.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            if (onSerchCustom != null)
            {
                onSerchCustom(frmCustomQuery.getCustomInfoRow);
            }
        }
        /// <summary>
        /// 根据字符串进行全文搜索
        /// </summary>
        /// <param name="dsCustomInfo"></param>
        /// <param name="serchStr"></param>
        /// <returns></returns>
        private DataTable searchCustomInfo(DataSet dsCustomInfo, string serchStr)
        {
            DataTable dtSearchCustom = dsCustomInfo.Tables[0].Copy();
            DataTable dtReturn = dtSearchCustom.Clone();
            //搜索姓名
            DataRow[] drSerchRows = serchFieldName(dtSearchCustom, DBEntityCustomInfo.FieldName.CNAME, serchStr);
            copyDataRow(drSerchRows, ref dtReturn);

            //生日
            drSerchRows = serchFieldName(dtSearchCustom, DBEntityCustomInfo.FieldName.CBIRTHDAY, serchStr);
            copyDataRow(drSerchRows, ref dtReturn);

            //联系电话1
            drSerchRows = serchFieldName(dtSearchCustom, DBEntityCustomInfo.FieldName.CMOBILEPHONE, serchStr);
            copyDataRow(drSerchRows, ref dtReturn);

            //联系电话2
            drSerchRows = serchFieldName(dtSearchCustom, DBEntityCustomInfo.FieldName.CFAMLIYPHONE, serchStr);
            copyDataRow(drSerchRows, ref dtReturn);

            //快速定位码
            drSerchRows = serchFieldName(dtSearchCustom, DBEntityCustomInfo.FieldName.CINDEXCODE, serchStr);
            copyDataRow(drSerchRows, ref dtReturn);


            return dtReturn;
        }
        /// <summary>
        /// 指定字段搜索数据集
        /// </summary>
        /// <param name="dtData"></param>
        /// <param name="serchFieldName"></param>
        /// <param name="serchValue"></param>
        /// <returns></returns>
        private DataRow[] serchFieldName(DataTable dtData, string serchFieldName, string serchValue)
        {
            DataTable dtReturn = dtData.Clone();
            DataRow[] drDatas = dtData.Select(DBEntityCustomInfo.FieldName.CSTATE + " = '" + DBEntityCustomInfo.ENUM_CSTATE.C0A + "' OR " 
                + DBEntityCustomInfo.FieldName.CSTATE + " = '" + DBEntityCustomInfo.ENUM_CSTATE.C0N + "'", "", DataViewRowState.CurrentRows);

            foreach (DataRow dr in drDatas)
            {
                if (dr[serchFieldName] == System.DBNull.Value)
                    continue;

                if (dr[serchFieldName].ToString().ToUpper().IndexOf(serchValue.ToUpper()) < 0)
                    continue;

                dtReturn.ImportRow(dr);
                dr.Delete();
            }
            dtReturn.AcceptChanges();
            dtData.AcceptChanges();
            return dtReturn.Select();
        }
        /// <summary>
        /// 对数据集行进行复制
        /// </summary>
        /// <param name="drCopyRow"></param>
        /// <param name="dtSearch"></param>
        private void copyDataRow(DataRow[] drCopyRow, ref DataTable dtSearch)
        {
            foreach (DataRow dr in drCopyRow)
            {
                dtSearch.ImportRow(dr);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            if (BussinessProxyManager.Instance != null)
            {
                if (!BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustomInfo, out error))
                {
                    MsgBox.MsgInfo(error);
                    return;
                }
            }
            if (dsCustomInfo != null)
            {
                dsSerchCustomInfo = dsCustomInfo.Copy();
            }
            MsgBox.MsgInfo("刷新完毕。");
        }


    }
}
